import com.mongodb.*;

import java.net.UnknownHostException;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * Created by zhaoji on 15/1/2.
 */
public class MongoDBBase {

    public static void main(String []args){
        try {
            //1.建立mongoDB数据库连接
            Mongo mongo = new Mongo("127.0.0.1:27017");
            //获取所有数据库的名称
            List<String> dbs = mongo.getDatabaseNames();
            for (String dbName : dbs){
                System.out.println(dbName);
            }
            //创建一个数据库
            DB db = mongo.getDB("zhaoji");
            //获取数据库中的所有集合
            Set<String> s = db.getCollectionNames();
            Iterator<String> it = s.iterator();
            System.out.println("----------------");
            while (it.hasNext()){
                String ss = it.next();
                System.out.println(ss);
            }

            //获取集合中的所有数据
            DBCollection dbc = db.getCollection("persons");
            DBCursor cursor = dbc.find();
            while (cursor.hasNext()){
                DBObject dbo = cursor.next();
                System.out.println(dbo.get("name"));
            }

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

    }
}
